package com.cloud.mall.user.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.ums.mapper.UmsResourceMapper;
import com.cloud.mall.ums.model.UmsResource;
import com.cloud.mall.ums.model.UmsResourceExample;
import com.cloud.mall.user.input.UmsResourceInpDTO;
import com.cloud.mall.user.output.UmsResourceOut;
import com.cloud.mall.usr.service.UmsResourceService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class UmsResourceServiceImpl implements UmsResourceService {
    @Autowired
    private UmsResourceMapper resourceMapper;


    @Override
    public BaseResponse create(@RequestBody UmsResourceInpDTO umsResourceInpDTO) {
        UmsResource umsResource = MallBeanUtils.dtoToDo(umsResourceInpDTO, UmsResource.class);
        umsResource.setCreateTime(new Date());
        int count=resourceMapper.insert(umsResource);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse update(Long id,  @RequestBody UmsResourceInpDTO umsResourceInpDTO) {
        UmsResource umsResource = MallBeanUtils.dtoToDo(umsResourceInpDTO, UmsResource.class);
        umsResource.setId(id);
        int count=resourceMapper.updateByPrimaryKeySelective(umsResource);
        //dynamicSecurityMetadataSource.clearDataSource();
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse delete(Long id) {
        int count= resourceMapper.deleteByPrimaryKey(id);
       // dynamicSecurityMetadataSource.clearDataSource();
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse<UmsResourceOut> getItem(Long id) {
        UmsResource umsResource = resourceMapper.selectByPrimaryKey(id);
        return BaseResponse.setResultSuccess(MallBeanUtils.doToDto(umsResource,UmsResourceOut.class));
    }

    @Override
    public BaseResponse<List<UmsResourceOut>> listAll() {
        List<UmsResource> resourceList=resourceMapper.selectByExample(new UmsResourceExample());
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(resourceList,UmsResourceOut.class));
    }

    @Override
    public BaseResponse<CommonPage<UmsResourceOut>> list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        UmsResourceExample example = new UmsResourceExample();
        UmsResourceExample.Criteria criteria = example.createCriteria();
        if(categoryId!=null){
            criteria.andCategoryIdEqualTo(categoryId);
        }
        if(StrUtil.isNotEmpty(nameKeyword)){
            criteria.andNameLike('%'+nameKeyword+'%');
        }
        if(StrUtil.isNotEmpty(urlKeyword)){
            criteria.andUrlLike('%'+urlKeyword+'%');
        }
        List<UmsResource> resourceList =resourceMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(resourceList,UmsResourceOut.class)));
    }
}
